package com.fs.wx;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 描述：
 */
@RefreshScope
@Service
public class WeWxPayService {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Value("${wx.notifyurl:http://mip.51tzb.com}")
    private String projectUrl;
    @Autowired
    private WeWxConfig weWxConfig;


    /**
     * 统一下单
     *
     * @return
     */
    public Map<String, String> unifiedOrder(String body,String orderId,String fee,String pid) throws Exception {
        Map<String, String> data = new HashMap();
        WXPay wxpay = new WXPay(weWxConfig);
        data.put("body", body);
        data.put("detail", body);
        data.put("out_trade_no", orderId);
        data.put("device_info", "WEB");
        data.put("fee_type", "CNY");
        data.put("total_fee", fee);
        data.put("spbill_create_ip", "127.0.0.1");
        data.put("notify_url", projectUrl + "/base/order/notifyUrl");
        data.put("trade_type", "NATIVE"); // 此处指定为扫码支付
        data.put("product_id", pid);
        try {
            Map<String, String> resp = wxpay.unifiedOrder(data);
            String s = new ObjectMapper().writeValueAsString(resp);
            logger.debug(s);
//      logger.debug(JSONObject.toJSONString(resp));
            return resp;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 订单查询
     *
     * @param orderNo:订单号
     * @return
     */
    public Map<String, String> orderQuery(String orderNo) throws Exception {
        Map<String, String> reqData = new HashMap();
        reqData.put("out_trade_no", orderNo);
        WXPay wxpay = new WXPay(weWxConfig);
        try {
            Map<String, String> resp = wxpay.orderQuery(reqData);
            String s = new ObjectMapper().writeValueAsString(resp);
            logger.debug(s);
            return resp;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getUrl() throws Exception {
        WXPay wxpay = new WXPay(new WeWxConfig());
        Map<String, String> data = new HashMap<String, String>();
        data.put("body", "上屏名称");
        data.put("detail", "商品详情");
        data.put("out_trade_no", "2ab9071b06b9f739b950ddb41db2690d");
        data.put("device_info", "");
        data.put("fee_type", "CNY");
        data.put("total_fee", "1");
        data.put("spbill_create_ip", "218.17.160.245");
        data.put("notify_url", "http://www.example.com/wxpay/notify");
        data.put("trade_type", "NATIVE"); // 此处指定为扫码支付
        data.put("product_id", "12");

        try {
            Map<String, String> resp = wxpay.unifiedOrder(data);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}